package operativa.model.dao;

import java.util.ArrayList;
import java.util.List;
import operativa.bean.entity.Cost;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class CostDAO extends GenericHibernateDAO<Cost, Integer> {

	@Override
	public void makeTransient(Integer id) {
		@SuppressWarnings("unused")
		Transaction tx =  super.getSession().beginTransaction();
		Cost oldCost = (Cost) super.getSession().get(Cost.class, id);
		super.getSession().delete(oldCost);
	}

	public Cost findCost(Integer ori_id, Integer dest_id) {
		List<Cost> result = this.findByCriteria(Restrictions.eq("origin.id", ori_id), 
				Restrictions.eq("destination.id", dest_id));		
		return (result.size() > 0 ? result.get(0) : null ); 
	}
	
	public List<Cost> findAllContaining(Integer containedId){
		List<Cost> result1 = this.findByCriteria(Restrictions.eq("origin.id", containedId));
		List<Cost> result2 = this.findByCriteria(Restrictions.eq("destination.id", containedId));
		List<Cost> finalResult = new ArrayList<Cost>();
		finalResult.addAll(result1);
		finalResult.addAll(result2);
		return finalResult;
		
	}

}
